package com.TCP_UDP网络编程;

import java.io.*;

public class StreamUtils {
    public static byte[] StreamToByte(InputStream is) throws IOException {

        byte[] bytes = new byte[1024];
        int readLine = 0;

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        while((readLine = is.read(bytes)) != -1){
            bos.write(bytes,0,readLine);
        }
        byte[] byteArray = bos.toByteArray();
        bos.close();
        is.close();

        return byteArray;
    }

    public static String StreamToString(InputStream is) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuilder b = new StringBuilder();
        String str;
        int readLine;
        while((str = br.readLine() )!= null){
            b.append(str+"\r\n");
        }
        br.close();
        return b.toString();
    }
}
